package com.briup.smartcity.web.interceptor;


import com.briup.smartcity.exception.ServiceException;

import com.briup.smartcity.mapper.extend.BaseUserExtendMapper;
import com.briup.smartcity.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class JwtInterceptor implements HandlerInterceptor {
//    @Autowired
//    BaseUserExtendMapper extendmapper;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String method = request.getMethod();
        if (method.equals("OPTIONS")){
            //不拦取
            return true;
        }
        String token = request.getHeader("Authorization");
        if(token == null){
            throw new ServiceException("用户未登录");
        }
        try{
            JwtUtil.checkSign(token);

        }catch (Exception e){
            e.printStackTrace();
           // extendmapper.updateloginoutstatus(JwtUtil.getUserId(token));
            throw new ServiceException("token无效，请重新登入");
        }
        return true;
    }

}
